package com.runfast.waimai.service.impl;

import com.runfast.common.service.BaseService;
import com.runfast.waimai.dao.mapper.RunfastCreditlevelTimeextractMapper;
import com.runfast.waimai.dao.model.RunfastCreditlevelTimeextract;
import com.runfast.waimai.dao.model.RunfastCreditlevelTimeextractExample;
import com.runfast.waimai.service.RunfastCreditlevelTimeextractService;
import com.runfast.waimai.util.MyUtil;
import com.runfast.waimai.util.RunFastSettingts;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Description:
 * @Auther: chenzheng
 * @Date: 2018/9/28 15:50
 **/
@Service
public class RunfastCreditlevelTimeextractServiceImpl extends BaseService<RunfastCreditlevelTimeextract, Integer, RunfastCreditlevelTimeextractExample> implements RunfastCreditlevelTimeextractService {

    @Resource
    RunfastCreditlevelTimeextractMapper timeextractDao;

    @Resource
    private RunFastSettingts runFastSettingts;

    @Override
    public List<RunfastCreditlevelTimeextract> get(Integer creditLevelid) {
        return timeextractDao.get(creditLevelid);
    }

    @Override
    public BigDecimal getShopperMoney(String distance, Double distan, BigDecimal price, BigDecimal maxprice, List<RunfastCreditlevelTimeextract> rclt) {

        BigDecimal shopperMoney=BigDecimal.valueOf(0);

        //添加本单收入
        //当前存在上班的时间

            for(RunfastCreditlevelTimeextract r:rclt){
                if(MyUtil.inTime(r.getStarttime(),r.getEndtime(), new Date())){
                    shopperMoney=r.getCommission();
                    break;
                }
            }

        //选取最近的上班时间
        if(shopperMoney.compareTo(BigDecimal.ZERO)==0){
            List<Long> l=new ArrayList<>();
            Date date = new Date();

            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            String dateStr = sdf.format(date);
            //System.out.println(dateStr);

            try {
                Date parse = sdf.parse(dateStr);//转到1970（当前时间）
                long time = parse.getTime();
                for(RunfastCreditlevelTimeextract r:rclt){
                    String format = sdf.format(r.getStarttime());//上班的开始时间
                    String format1 = sdf.format(r.getEndtime());//上班的结束时间
                    Date parse1 = sdf.parse(format);
                    Date parse2 = sdf.parse(format1);
                    long la = time - parse1.getTime();
                    long lb = time - parse2.getTime();
                    l.add(Math.abs(la));
                    l.add(Math.abs(lb));
                }

                //求出与上班时间最接近的时间段
                Long min = Collections.min(l);
                //System.out.println("--------------"+min);
                if(l.contains(min)){
                    int i = l.indexOf(min);
                    //Long aLong = l.get(i / 2);

                    int index = i / 2;
                    if(rclt.size()>index){
                        RunfastCreditlevelTimeextract runfastCreditlevelTimeextract = rclt.get(i/2);
                        shopperMoney=runfastCreditlevelTimeextract.getCommission();
                    }

                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }



        if(distance!=null){
            //// 配送距离   ;//超出多少公里后额外加钱 //额外每公里加多少钱
            if(distance!=null&&distan!=null&&price!=null){//distan超出多少公里后额外加钱
                Double distans=distan; //超出多少公里后额外加钱
                if(Double.parseDouble(distance)>distan*1000){
                    //当超出了范围后，计算超出的费用
                    double ss =  Double.parseDouble(distance) - distan*1000;
                    ss = ss /1000;
                    if(ss%1!=0){
                        ss=  ss+1;
                    }
                    int sd = (int)ss;
                    BigDecimal priceshopper = price.multiply(BigDecimal.valueOf(sd));  //额外每公里加多少钱
                    shopperMoney=shopperMoney.add(priceshopper);
                    if(maxprice!=null){
                        if(shopperMoney.compareTo(maxprice)==1){
                            shopperMoney = maxprice;
                        }
                    }
                }
            }
        }
        return shopperMoney;
    }


}